package org.dyz.banana.platform.util.mappergen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dyz.banana.platform.util.mappergen.constant.ValueConstant;
import org.dyz.banana.platform.util.mappergen.fileoutconfig.MapperFileOutConfig;
import org.dyz.banana.platform.util.mappergen.fileoutconfig.MapperXmlFileOutConfig;
import org.dyz.banana.platform.util.mappergen.fileoutconfig.ServiceFileOutConfig;
import org.dyz.banana.platform.util.mappergen.fileoutconfig.ServiceImplFileOutConfig;
import org.dyz.banana.platform.util.mappergen.fileoutconfig.VoFileOutConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.FileType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

@Component
public class BananaMbgAutoGenerator extends AutoGenerator{

	Logger logger = LoggerFactory.getLogger(BananaMbgAutoGenerator.class);
	
	public BananaMbgAutoGenerator setDefaultGlobalConfig(String outputDir) {
		GlobalConfig globalConfig = new GlobalConfig();
	        globalConfig.setOutputDir(outputDir + "\\src\\main\\java")
	        .setDateType(DateType.SQL_PACK)
	        .setAuthor("dongyongzhao")   // 作者
//	        .setServiceName("%sService");  // 设置Service接口生成名称,这样生成接口前面就不会有 I
	          .setOpen(false); //生成代码后是否打开文件夹
	     this.setGlobalConfig(globalConfig);
	     return this;
	}
	
	public BananaMbgAutoGenerator setDefaultDataSourceConfig(String url,
			String driver,String username,String password) {
		 DataSourceConfig dsc = new DataSourceConfig();
	        dsc.setUrl(url)
	        	.setDriverName(driver)
	        	.setUsername(username)
	        	.setPassword(password);
	        this.setDataSource(dsc);
	        return this;
	}
	/**
	 * 配置各个模块（Entity、Mapper...）生成的位置
	 * @param parentName
	 * @param moduleName
	 * @param fileTypeEnum
	 * @return
	 */
	public BananaMbgAutoGenerator setDefaultPackageConfig(String parentName, String moduleName, FileType... fileTypeEnum) {
       PackageConfig packageConfig = new PackageConfig();
       //父模块路径
       packageConfig.setParent(parentName);
       //子模块名
       packageConfig.setModuleName(moduleName);
       if (fileTypeEnum.length == 0) {
           return this;
       }
       packageConfig.setEntity("");
       packageConfig.setMapper("");
       packageConfig.setXml("");
       packageConfig.setService("");
       packageConfig.setServiceImpl("");
       packageConfig.setController("");
       for (int i = 0; i < fileTypeEnum.length; i++) {
           if (fileTypeEnum[i] == FileType.ENTITY)
               packageConfig.setEntity("entity");
           else if (fileTypeEnum[i] == FileType.MAPPER)
               packageConfig.setMapper("mapper");
           else if (fileTypeEnum[i] == FileType.XML)
               packageConfig.setXml("mapper.xml");
           else if (fileTypeEnum[i] == FileType.SERVICE)
               packageConfig.setService("service");
           else if (fileTypeEnum[i] == FileType.SERVICE_IMPL)
               packageConfig.setServiceImpl("service.impl");
           else if (fileTypeEnum[i] == FileType.CONTROLLER)
               packageConfig.setController("controller");
       }
       this.setPackageInfo(packageConfig);
       return this;
   }
	/**
	 * 选择生成策略
	 * @param tableName
	 * @return
	 */
	public BananaMbgAutoGenerator setDefaultStrategyConfig(String ...tableName ) {
		StrategyConfig strategy = new StrategyConfig();
		// 驼峰式命名法
       strategy.setNaming(NamingStrategy.underline_to_camel)
       	.setColumnNaming(NamingStrategy.underline_to_camel)
       	.setEntityLombokModel(false)
       	.setEntityTableFieldAnnotationEnable(true)
       	// 要生成的表名
       	.setInclude(tableName);  
       this.setStrategy(strategy);
       return this;
	}
	/**
	 * 过滤不需要生成代码的模块（Entity、Mapper、XML、Service、ServiceImpl、Controller）
	 * @param fileTypeEnum
	 * @return
	 */
	public BananaMbgAutoGenerator setDefaultTemplateConfig(FileType... fileTypeEnum) {
		TemplateConfig templateConfig = new TemplateConfig();
		List<FileType> fileTypes = new ArrayList<FileType>();
		fileTypes.add(FileType.ENTITY);
		fileTypes.add(FileType.MAPPER);
		fileTypes.add(FileType.XML);
		fileTypes.add(FileType.SERVICE);
		fileTypes.add(FileType.SERVICE_IMPL);
		fileTypes.add(FileType.CONTROLLER);
		for (int i = 0; i < fileTypeEnum.length; i++) {
			fileTypes.remove(fileTypeEnum[i]);
		}
		//过滤掉XML、Mapper、Servce和ServiceImpl，改为在Inject处生成
		fileTypes.add(FileType.SERVICE);
		fileTypes.add(FileType.SERVICE_IMPL);
		fileTypes.add(FileType.MAPPER);
		fileTypes.add(FileType.XML);
		
		for (int i = 0; i < fileTypes.size(); i++) {
	           if (fileTypes.get(i) == FileType.ENTITY)
	        	   templateConfig.setEntity(null);
	           else if (fileTypes.get(i) == FileType.MAPPER)
	        	   templateConfig.setMapper(null);
	           else if (fileTypes.get(i) == FileType.XML)
	        	   templateConfig.setXml(null);
	           else if (fileTypes.get(i) == FileType.SERVICE)
	        	   templateConfig.setService(null);
	           else if (fileTypes.get(i) == FileType.SERVICE_IMPL)
	        	   templateConfig.setServiceImpl(null);
	           else if (fileTypes.get(i) == FileType.CONTROLLER)
	        	   templateConfig.setController(null);
	    }
		this.setTemplate(templateConfig);
		return this;
	}
	
	public BananaMbgAutoGenerator setInjectionConfig() {
		InjectionConfig config = new BananaInjectionConfig();
		this.injectionConfig=config;
		return this;
	}
	
	//自定义生成VO、Service、ServiceImpl
	private class BananaInjectionConfig extends InjectionConfig{

		public BananaInjectionConfig() {
			List<FileOutConfig> fileOutConfigList=new ArrayList<FileOutConfig>();
			String voPath =ValueConstant.WORKSPACE_DIR+ValueConstant.COMMON + "\\src\\main\\java" +
					"\\org\\dyz\\banana\\platform\\common\\vo";
			FileOutConfig focConfig =new VoFileOutConfig(voPath);
			fileOutConfigList.add(focConfig);
			String servicePath =ValueConstant.WORKSPACE_DIR+ValueConstant.COMMON + "\\src\\main\\java" +
					"\\org\\dyz\\banana\\platform\\common\\service";
			FileOutConfig serviceConfig =new ServiceFileOutConfig(servicePath);
			fileOutConfigList.add(serviceConfig);
			String serviceimplPath =ValueConstant.WORKSPACE_DIR+ValueConstant.COMMON + "\\src\\main\\java" +
					"\\org\\dyz\\banana\\platform\\common\\service\\impl";
			FileOutConfig serviceImplConfig =new ServiceImplFileOutConfig(serviceimplPath);
			fileOutConfigList.add(serviceImplConfig);
			String xmlPath =ValueConstant.WORKSPACE_DIR+ValueConstant.COMMON + "\\src\\main\\java" +
					"\\org\\dyz\\banana\\platform\\common\\xml";
			FileOutConfig xmlConfig =new MapperXmlFileOutConfig(xmlPath);
			fileOutConfigList.add(xmlConfig);
			String mapperPath =ValueConstant.WORKSPACE_DIR+ValueConstant.COMMON + "\\src\\main\\java" +
					"\\org\\dyz\\banana\\platform\\common\\mapper";
			FileOutConfig mapperConfig =new MapperFileOutConfig(mapperPath);
			fileOutConfigList.add(mapperConfig);
			this.setFileOutConfigList(fileOutConfigList);
		}
		@Override
		public void initMap() {
			HashMap<String , Object> map = new HashMap<String , Object>();
			map.put("vo","org.dyz.banana.platform.common.vo");
			map.put("posefix", "Vo");
			setMap(map);
			
		}
		
	}
}
